package com.yanqu.road.server.cmd.activity.stronghold.wild;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.stronghold.StrongholdWarUserData;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.strongholdwar.StrongholdModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.*;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = GameProtocol.S_STRONGHOLD_WAR_DEAL_EVENT, desc = "处理事件")
public class StrongholdWildDealEventCmd extends AbstractCommand {
    private boolean deal;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.DealStrongholdEventReqMsg req = StrongholdWarProto.DealStrongholdEventReqMsg.parseFrom(packet.getBytes());
        deal = req.getDeal();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        //直接发到跨服处理
        ActivityInfo activityInfo = StrongholdActivityMgr.getActivityInfo();
        if (activityInfo == null) {
            ret = GameErrorCode.E_STRONGHOLD_NOT_FOUND;
            sendToPlayer(player);
            return;
        }
        if (deal) {
            StrongholdWarUserData userData = player.getModule(StrongholdModule.class).getStrongholdWarData(activityInfo.getActivityId());
            player.getModule(StrongholdModule.class).calcPower(userData);

            String consume = userData.getEvent();
            if (consume != null) {
                if (!player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(consume), eLogMoneyType.Stronghold, eLogMoneyType.StrongholdDealEvent)) {
                    ret = GameErrorCode.E_BAG_SILVER_NO_ENOUGH;
                    sendToPlayer(player);
                    return;
                }
            }
        }
        StrongholdWarProto.CrossDealStrongholdEventReqMsg.Builder req = StrongholdWarProto.CrossDealStrongholdEventReqMsg.newBuilder();
        req.setActivityId(activityInfo.getActivityId());
        req.setDeal(deal);
        player.sendUnionActivityGroupPacket(CrossProtocol.C_CROSS_STRONGHOLD_WILD_DEAL_EVENT, req, req.getActivityId());
    }

    private void sendToPlayer(GamePlayer player) {
        StrongholdWarProto.DealStrongholdEventResp.Builder resp = StrongholdWarProto.DealStrongholdEventResp.newBuilder();
        resp.setRet(ret);
        resp.setDeal(deal);
        player.sendPacket(ClientProtocol.U_STRONGHOLD_WAR_DEAL_EVENT,resp);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
